package com.hw.one.peach.core.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hw.one.core.controller.BaseController;
import com.hw.one.core.result.MsgResult;
import com.hw.one.ext.util.UserUtil;
import com.hw.one.peach.core.service.IMovingSourceService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by Administrator on 2017/8/10.
 *
 * 移动源 控制类
 * 包括车辆，移动源
 */
@RestController
@RequestMapping(path = "movingSource")
public class MovingSourceController extends BaseController{

    // 移动源
    @Resource
    private IMovingSourceService movingSourceService;

    /**
     * 查询移动源数据
     * @author 李亚坤
     * @createtime 2017年8月10日11:10:06
     * @return
     */
    @RequestMapping(value = "/findMovingSource", method = RequestMethod.GET)
    public MsgResult findMovingSource(HttpServletRequest request){
        try {

            Map<String, Object> region = UserUtil.getRegion();
            String userRegionCode = (String) region.get("regionCode");   //当前用户所在的区域
            String userRegionName = (String) region.get("regionName");
            // 车辆类型
            String deviceType = request.getParameter("deviceType");
            // 车辆状态
            String deviceStatus = request.getParameter("deviceStatus");
            String carCodeLike = request.getParameter("carCodeLike");
            String regionCode = request.getParameter("regionCode");
            // 查询所有移动源
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("regionCode", StringUtils.isEmpty(regionCode) ? userRegionCode : regionCode);
            paramMap.put("regionName", userRegionName);
            paramMap.put("deviceType", deviceType == null ? "0" : deviceType);
            paramMap.put("deviceStatus", StringUtils.isEmpty(deviceStatus) ? null : deviceStatus);
            paramMap.put("carCodeLike", carCodeLike);

            Object obj = movingSourceService.findMovingSource(paramMap);

            return MsgResult.okResult(obj);
        }catch (Exception e) {
            log.error("查询移动源数据异常，后台异常", e);
            return MsgResult.exceptionResult();
        }
    }

    /**
     * 查询移动源数据
     * @author 李亚坤
     * @createtime 2017年8月10日11:10:06
     * @return
     */
    @RequestMapping(value = "/findMovingSourceByLoc", method = RequestMethod.GET)
    public MsgResult findMovingSourceByLoc(HttpServletRequest request){
        try {
            Map<String, Object> map = wrapParams(request);
            Map<String, Object> region = UserUtil.getRegion();
            String userRegionCode = (String) region.get("regionCode");   //当前用户所在的区域
            String userRegionName = (String) region.get("regionName");

            // 车辆类型
            String deviceType = (String) map.get("deviceType");
            // 车辆状态
            String deviceStatus = (String) map.get("deviceStatus");
            String carCodeLike = (String) map.get("carCodeLike");
            String regionCode = (String) map.get("regionCode");
            String radius = (String) map.get("radius");
            String longitude = (String) map.get("longitude");
            String latitude = (String) map.get("latitude");

            // 查询所有移动源
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("regionCode", StringUtils.isEmpty(regionCode) ? userRegionCode : regionCode);
            paramMap.put("regionName", userRegionName);
            // 数据为空，从session中取值
            if (StringUtils.isEmpty(longitude) && StringUtils.isEmpty(latitude)) {
                longitude =String.valueOf(region.get("longitude"));
                latitude = String.valueOf(region.get("latitude"));
            }
            paramMap.put("longitude", longitude);
            paramMap.put("latitude", latitude);
            paramMap.put("radius", StringUtils.isEmpty(radius) ? "3000" : radius);
            paramMap.put("deviceType", deviceType == null ? "0" : deviceType);
            paramMap.put("deviceStatus", StringUtils.isEmpty(deviceStatus) ? null : deviceStatus);
            paramMap.put("carCodeLike", carCodeLike);

            Object obj = movingSourceService.findMovingSourceByLoc(paramMap);

            return MsgResult.okResult(obj);
        }catch (Exception e) {
            log.error("查询移动源数据异常，后台异常", e);
            return MsgResult.exceptionResult();
        }
    }


    /**
     * 查询车辆状态信息
     * @author 李亚坤
     * @createtime 2017年8月10日13:46:29
     * @return
     */
    @RequestMapping(value = "findCarStatus", method = RequestMethod.GET)
    public MsgResult findCarStatus() {
        try {
            JSONArray returnArr = new JSONArray();
            JSONObject returnObj = new JSONObject();
            returnObj.put("carStatus", "全部");
            returnObj.put("statusCode", "-1");
            returnArr.add(returnObj);
            returnObj = new JSONObject();
            returnObj.put("carStatus", "本地待命");
            returnObj.put("statusCode", "0");
            returnArr.add(returnObj);
            returnObj = new JSONObject();
            returnObj.put("carStatus", "维修");
            returnObj.put("statusCode", "1");
            returnArr.add(returnObj);
            returnObj = new JSONObject();
            returnObj.put("carStatus", "事故");
            returnObj.put("statusCode", "2");
            returnArr.add(returnObj);
            returnObj = new JSONObject();
            returnObj.put("carStatus", "停用");
            returnObj.put("statusCode", "3");
            returnArr.add(returnObj);
            returnObj = new JSONObject();
            returnObj.put("carStatus", "短盘");
            returnObj.put("statusCode", "4");
            returnArr.add(returnObj);
            returnObj = new JSONObject();
            returnObj.put("carStatus", "审车");
            returnObj.put("statusCode", "5");
            returnArr.add(returnObj);
            returnObj = new JSONObject();
            returnObj.put("carStatus", "营运");
            returnObj.put("statusCode", "6");
            returnArr.add(returnObj);
            returnObj = new JSONObject();
            returnObj.put("carStatus", "异地待命");
            returnObj.put("statusCode", "7");
            returnArr.add(returnObj);
            returnObj = new JSONObject();
            returnObj.put("carStatus", "停运");
            returnObj.put("statusCode", "8");
            returnArr.add(returnObj);
            returnObj = new JSONObject();
            returnObj.put("carStatus", "禁运");
            returnObj.put("statusCode", "9");
            returnArr.add(returnObj);
            return MsgResult.okResult(returnArr);
        }catch (Exception e) {
            log.error("查询车辆状态异常", e);
            return MsgResult.exceptionResult();
        }
    }

    /**
     * 查询框选区域的车辆信息
     * @author 李亚坤
     * @createtime 2017年8月10日16:22:16
     * @param request
     * @return
     */
    @RequestMapping(value = "findSelectBoxCar", method = RequestMethod.GET)
    public MsgResult findSelectBoxCar(HttpServletRequest request) {
        try {
            Map<String, Object> map = wrapParams(request);
            String lngSets = (String) map.get("lngSets");
            String latSets = (String) map.get("latSets");
            String deviceType = (String) map.get("deviceType");
            String deviceStatus = (String) map.get("deviceStatus");
            String startTime = (String) map.get("startTime");
            String endTime = (String) map.get("endTime");
            String regionCode = (String) map.get("regionCode");

            Map<String, Object> region = UserUtil.getRegion();
            String userRegionCode = (String) region.get("regionCode");   //当前用户所在的区域
            String userRegionName = (String) region.get("regionName");
            if (StringUtils.isEmpty(lngSets) || StringUtils.isEmpty(latSets)) {
                return MsgResult.errorResult("框选区域经纬度集合不能为空");
            }

            Map<String, Object> paramMap = new HashMap<>();

            paramMap.put("startTime", startTime);
            paramMap.put("endTime", endTime);
            paramMap.put("latSets", latSets);
            paramMap.put("lngSets", lngSets);
            paramMap.put("deviceType", deviceType == null ? "0" : deviceType);
            paramMap.put("deviceStatus", StringUtils.isEmpty(deviceStatus) ? null : deviceStatus);
            paramMap.put("regionCode", StringUtils.isEmpty(regionCode) ? userRegionCode : regionCode);
            paramMap.put("regionName", userRegionName);

            Object obj = movingSourceService.findSelectBoxCar(paramMap);

            return MsgResult.okResult(obj);
        }catch (Exception e) {
            log.error("查询框选区域的车辆信息", e);
            return MsgResult.exceptionResult();
        }
    }


    /**
     * 查询移动源数据
     * @author 李亚坤
     * @createtime 2017年8月10日11:10:06
     * @return
     */
    @RequestMapping(value = "/findCarInfo", method = RequestMethod.GET)
    public MsgResult findCarInfo(HttpServletRequest request){
        try {
            Map<String, Object> region = UserUtil.getRegion();
            String userRegionCode = (String) region.get("regionCode");   //当前用户所在的区域
            String userRegionName = (String) region.get("regionName");
            String carCode = request.getParameter("carCodeLike");
            String regionCode = request.getParameter("regionCode");
            // 车辆状态
            String deviceStatus = request.getParameter("deviceStatus");
            // 查询所有移动源
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("regionCode", StringUtils.isEmpty(regionCode) ? userRegionCode : regionCode);
            paramMap.put("regionName", userRegionName);
            paramMap.put("deviceType", "0");
            paramMap.put("carCodeLike", carCode);

            Object obj = movingSourceService.findMovingSource(paramMap);

            return MsgResult.okResult(obj);
        }catch (Exception e) {
            log.error("查询移动源数据异常，后台异常", e);
            return MsgResult.exceptionResult();
        }
    }
}
